﻿using MailKit.Search;
using PHMEE.Admin.Application.Contracts.IServices.DaqInfo.WaveIntervals;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping;

namespace PHMEE.Admin
{
    /// <summary>
    /// 波形、指标间隔服务类
    /// </summary>
    public class WaveIntervalAppSerive : PHMEEAdminAppService, IWaveIntervalAppSerive
    {
        private readonly IRepository<WaveInterval, Guid> _waveintervalRepository;
        private IGuidGenerator _guidGenerator;

        public WaveIntervalAppSerive(IRepository<WaveInterval, Guid> waveintervalRepository, IGuidGenerator guidGenerator)
        {
            _waveintervalRepository = waveintervalRepository;
            _guidGenerator = guidGenerator;
        }
        /// <summary>
        /// 根据id查询间隔
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WaveIntervalDto> GetByIdAsync(Guid id)
        {
            var waveInterval = await _waveintervalRepository.GetAsync(id);
            return ObjectMapper.Map<WaveInterval, WaveIntervalDto>(waveInterval);
        }
        /// <summary>
        /// 根据间隔类型查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<WaveIntervalDto>> GetByTypeAsync(IntervalStyle type)
        {
            var queryable = await _waveintervalRepository.GetQueryableAsync();
            var waveIntervals = await AsyncExecuter.ToListAsync(queryable
                .Where(item => item.IntervalStyle == type)
                .OrderBy(item => item.IntervalUnit)
                .ThenBy(item=>item.TimeSpan));

            return ObjectMapper.Map<List<WaveInterval>, List<WaveIntervalDto>>(waveIntervals);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<WaveIntervalDto>> GetPagedAsync(PageWaveIntevalDto input)
        {
            var queryable = await _waveintervalRepository.GetQueryableAsync();
            //判断是否按照类型查询
            if (input.IntervalStyle.HasValue)
            {
                queryable = queryable.Where(item => item.IntervalStyle == input.IntervalStyle.Value);
            }
            queryable = queryable.OrderBy(x => x.IntervalStyle).ThenBy(x => x.IntervalUnit).ThenBy(x => x.TimeSpan);
            //判断是否分页
            if (input.IsPaged)
            {
                queryable = queryable.Skip(input.SkipCount).Take(input.MaxResultCount);
            }
            var waveIntervals = await AsyncExecuter.ToListAsync(queryable);

            var totalCount = await _waveintervalRepository.GetCountAsync();

            return new PagedResultDto<WaveIntervalDto>(totalCount, ObjectMapper.Map<List<WaveInterval>, List<WaveIntervalDto>>(waveIntervals));
        }
        /// <summary>
        /// 创建波形，指标间隔
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<WaveIntervalDto> CreateAsync(CreateWaveIntervalDto input)
        {
            Guid id = _guidGenerator.Create();
            var waveInterval = new WaveInterval(id);
            ObjectMapper.Map(input, waveInterval);
            await _waveintervalRepository.InsertAsync(waveInterval, autoSave: true);
            var indexWave = await _waveintervalRepository.GetAsync(id);
            return ObjectMapper.Map<WaveInterval, WaveIntervalDto>(indexWave);
        }
        /// <summary>
        /// 更新波形，指标间隔数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(Guid id, UpdateWaveIntervalDto input)
        {
            var waveInterval = await _waveintervalRepository.GetAsync(id);
            ObjectMapper.Map(input, waveInterval);
        }
        /// <summary>
        /// 删除波形，指标间隔数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _waveintervalRepository.DeleteAsync(id);
        }
    }
}
